﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;

namespace Web.Models
{
    public class Reserva
    {
        [Column("id")]
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public long IdReserva { get; set; }

        [Column("codigoReserva")]
        public string CodigoReserva { get; set; }

        [Column("fechaIngreso")]
        public DateTime FechaIngreso { get; set; }

        [Column("fechaEgreso")]
        public DateTime FechaEgreso { get; set; }

        public virtual Habitacion Habitacion { get; set; }

        public virtual Categoria Categoria { get; set; }

        public virtual Cliente Cliente { get; set; }

        public Reserva() { }

        public void GenerarCodigoReserva()
        {
            string palabra = Cliente.Dni;
            string hashed = "";

            for (int i = 0; i < palabra.Length; i++)
            {
                if (i % 2 == 0)
                {
                    hashed += IntToLetter(palabra[i]);
                }
                else
                {
                    hashed += palabra[i];
                }
            }
            CodigoReserva = hashed;                
        }

        private char IntToLetter(char number)
        {
            char letter; 
            switch (number)
            {
                case '1':
                    letter = 'L';
                    break;
                case '2':
                    letter = 'N';
                    break;
                case '3':
                    letter = 'M';
                    break;
                case '4':
                    letter = 'C';
                    break;
                case '5':
                    letter = 'S';
                    break;
                case '6':
                    letter = 'B';
                    break;
                case '7':
                    letter = 'T';
                    break;
                case '8':
                    letter = 'G';
                    break;
                case '9':
                    letter = 'P';
                    break;
                case '0':
                    letter = 'D';
                    break;
                default:
                    letter = 'X';
                    break;
            }
            return letter;
        }

        public void EnviarConfirmacion()
        {
            string emailDestino = this.Cliente.Email;
            string emailOrigen = "superhotelucho@gmail.com";
            string password = "hotelucho123";

            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

            smtpClient.Credentials = new System.Net.NetworkCredential(emailOrigen, password);
            smtpClient.UseDefaultCredentials = true;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = true;

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(emailOrigen, "Super Hotelucho");
            mail.To.Add(emailDestino);
            mail.CC.Add("mariano.aquino@gmail.com");
            mail.Subject = "Sarasa";
            mail.Body = "lalalalala lalalalala lalalalala lalalalala";

            smtpClient.Send(mail);
        }
    }
}